package com.simple.gateway.sdk.application;

import com.alibaba.fastjson.JSON;
import com.simple.gateway.sdk.annotation.ApiProducerClazz;
import com.simple.gateway.sdk.annotation.ApiProducerMethod;
import com.simple.gateway.sdk.config.GatewaySDKServiceProperties;
import com.simple.gateway.sdk.exception.GatewayException;
import com.simple.gateway.sdk.service.GatewayCenterService;
import com.simple.gateway.sdk.service.param.ApplicationInterfaceMethodParam;
import com.simple.gateway.sdk.service.param.ApplicationInterfaceParam;
import com.simple.gateway.sdk.service.param.ApplicationSystemParam;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Method;

/**
 * 项目: simple-gateway-core
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2023-08-09 17:58
 **/
public class GatewaySDKApplication implements BeanPostProcessor {

    private final GatewaySDKServiceProperties properties;
    private final GatewayCenterService gatewayCenterService;

    public GatewaySDKApplication(GatewaySDKServiceProperties properties, GatewayCenterService gatewayCenterService) {
        this.properties = properties;
        this.gatewayCenterService = gatewayCenterService;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        ApiProducerClazz apiProducerClazz = bean.getClass().getAnnotation(ApiProducerClazz.class);
        if (null == apiProducerClazz) {
            return bean;
        }
        // 1. 系统信息
        gatewayCenterService.doRegisterApplication(properties.getAddress(), new ApplicationSystemParam(properties.getSystemId(),
                properties.getSystemName(), properties.getSystemType(),
                properties.getSystemRegistry()));

        // 2. 接口信息
        Class<?>[] interfaces = bean.getClass().getInterfaces();
        if (interfaces.length != 1) {
            throw new GatewayException(bean.getClass().getName() + "interfaces not one this is " + JSON.toJSONString(interfaces));
        }
        String interfaceId = interfaces[0].getName();
        gatewayCenterService.doRegisterApplicationInterface(properties.getAddress(),
                new ApplicationInterfaceParam(properties.getSystemId(),
                        interfaceId,
                        apiProducerClazz.interfaceName(),
                        apiProducerClazz.interfaceType(),
                        apiProducerClazz.interfaceVersion()));

        // 3. 方法信息
        Method[] methods = bean.getClass().getMethods();
        for (Method method : methods) {
            ApiProducerMethod apiProducerMethod = method.getAnnotation(ApiProducerMethod.class);
            if (apiProducerMethod == null) {
                continue;
            }
            // 解析参数
            Class<?>[] parameterTypes = method.getParameterTypes();
            StringBuilder parameters = new StringBuilder();
            for (Class<?> clazz : parameterTypes) {
                parameters.append(clazz.getName()).append(",");
            }
            String parameterType = parameters.substring(0, parameters.toString().lastIndexOf(","));

            gatewayCenterService.doRegisterApplicationInterfaceMethod(properties.getAddress(),
                    new ApplicationInterfaceMethodParam(
                            properties.getSystemId(),
                            interfaceId,
                            method.getName(),
                            apiProducerMethod.methodName(),
                            parameterType,
                            apiProducerMethod.uri(),
                            apiProducerMethod.httpCommandType(),
                            apiProducerMethod.auth()));
        }

        // 注册完成，执行事件通知
        gatewayCenterService.doRegisterEvent(properties.getAddress(), properties.getSystemId());

        return bean;
    }
}
